package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Vipcard;
import com.gxa.zhonglong.service.VipcardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther JJuventus
 * @date 2021/10/26 10:09
 */

@RestController
@RequestMapping("/vipcard")
@Api(value = "会员卡管理")
@CrossOrigin
@Transactional(rollbackFor = Throwable.class)
public class VipcardController {

    @Autowired
    private VipcardService vipcardService = null;

    @RequestMapping("/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "int")
    })
    public Map select(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        List<Vipcard> vipcards = vipcardService.select(page, size);
        List<Vipcard> all = vipcardService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",all.size());
        jsonResult.setData(vipcards);
        return jsonResult.getDataMap();
    }

    @RequestMapping("/add")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "vipcard",value = "添加的会员卡", required = true,paramType = "body")
    )
    public Map add(@RequestBody Vipcard vipcard) throws BusinessException {
        vipcardService.add(vipcard);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/delete")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "vipcardId",value = "需要删除的会员卡的id号",required = true,dataType = "long")
    )
    public Map delete(@RequestParam("vipcardId") Long vipcardId) throws BusinessException {
        vipcardService.delete(vipcardId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/update")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "vipcard",value = "修改的会员卡", required = true,paramType = "body")
    )
    public Map update(@RequestBody Vipcard vipcard) throws BusinessException {
        vipcardService.update(vipcard);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
